<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" type="info" size="small" round icon="search" to="/search">
        搜索
      </van-button>
    </van-nav-bar>
    <van-tabs class="home-tab" v-model="active" animated swipeable>
      <van-tab 
      v-for="obj in channels"
      :title="obj.name"
      :key="obj.id">
      <articleList :channels="obj"/>
      </van-tab>
    
      <div slot="nav-right" class="placeholder"></div>
      <!-- 更多按钮 -->
      <div slot="nav-right" class="gengduo" @click="show=true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup v-model="show"
      close-icon-position="top-left"
      closeable
      position="bottom" 
      :style="{ height: '100%' }" 
    >
    <channel-edit :myChannels="channels" :active="active" @change="change"></channel-edit>
    </van-popup>
  </div>
</template>
  
<script>
import {getChannelsAPI} from '@/api'
import ArticleList from './components/ArticleList'
import ChannelEdit from './components/ChannelEdit'
import { mapState } from 'vuex'
import {getItem} from '@/utils/storage'



export default {
  name:'home',
  data(){
    return {
      active:0,
      channels:[],
      show:false,//控制频道编辑弹出层是否显示
    }
  },
  components:{
    ArticleList,
    ChannelEdit
  },
 created(){
    this.loadChannels()
  },
  methods:{
    async loadChannels(){
      try{
        // const {data}= await getChannelsAPI()
        // this.channels=data.data.channels
        let channels=[]
        if(this.user){  //已登录
          const {data}= await getChannelsAPI()
          channels=data.data.channels
        }else{  //未登录
          const localChannels=getItem('TOUTIAO_CHANNELS')
          if(localChannels){
            channels= localChannels
          }else{
            //请求获取默认频道列表
            const {data}= await getChannelsAPI()
            channels=data.data.channels
          }
        }
        this.channels=channels
      }catch(err){
        this.$toast('获取频道数据失败')
      }
    },
    change(index,defaultValue=true){
      this.active =index
      this.show=defaultValue
    }
  },
  computed:{
    ...mapState(['user'])
  }

}
</script>

<style scoped lang="less">
.home-container{
  padding-top: 179px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title{
    max-width: unset;
  }
  .search-btn{
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: 0;
    font-size: 28px;
    .van-icon-search{
      font-size: 32px;
    }
  }
  .placeholder{
    width: 66px;
    height: 82px;
    flex-shrink: 0;
  }
  /deep/.home-tab{
    .van-tabs__wrap{
      height: 82px;
    }
    .van-tab{
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active{
      color: #333;
    }
    .van-tabs__line{
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
    .van-tabs__nav{
      padding-bottom: 0;
    }
    .gengduo{
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.5;
      i.iconfont{
        font-size: 33px;
      }
      &:before{
        position: absolute;
        left: 0;
        background-size: contain;
        content:"";
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
      }
    }
  }
  /deep/.van-tabs__wrap{
  position: fixed;
  top: 92px;
  z-index: 1;
  left: 0;
  right: 0;
  background-color: rgb(230, 56, 56);
}
// /deep/.van-list{
//   height: 80vh;
//   overflow-y: auto;
// }

}

</style>